#include "lrcpage.h"
#include "ui_lrcpage.h"
#include<QPropertyAnimation>
#include<QFile>
#include<QDebug>


LrcPage::LrcPage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::LrcPage)
{
    ui->setupUi(this);
    setWindowFlag(Qt::FramelessWindowHint);
    setGeometry(x()+10,y()+10,width(),height());
    ui->hideBtn->setIcon(QIcon(":/image/hide.png"));
    ui->hideBtn->setStyleSheet("background-color:green");
}

LrcPage::~LrcPage()
{
    delete ui;
}

void LrcPage::on_hideBtn_clicked()
{
    QPropertyAnimation* animation=new QPropertyAnimation(this,"geometry",this);
    animation->setDuration(1000);//设置动画的持续时长

    animation->setStartValue(QRect(10,10,width(),height()));
    animation->setEndValue(QRect(10,10+height(),width(),height()));
    animation->start();
    connect(animation,&QPropertyAnimation::finished,this,[=]{delete animation;hide();});


}

bool LrcPage::phraseLrcfile(const QString &lrcFilePath)
{
    //每次解析之前得清空之前的元素

    _LrcLines.clear();
    //打开文件
    QFile file(lrcFilePath);
    if(!file.open(QIODevice::ReadOnly))
    {
        qDebug()<<"文件"<<":"<<lrcFilePath<<"打开失败";
        return false;
    }
    //判断文件读到末尾没,没读到就继续读
    while(!file.atEnd())
    {
        //按行读取内容,默认读取1kb的大小
        QString line=file.readLine(1024);
        qDebug()<<line;
        //对行进行解析,lrc文件格式如下
        // [00:17.94]那些失眠的⼈啊 你们还好吗\n
        // [0:58.600.00]你像⼀只⻜来⻜去的蝴蝶\n

        qint64 totaltime=0;
        QString text;
        QString time;
        int left=line.indexOf('['),right=line.indexOf(']');//使用两个变量来进行截取
        if(right==-1)
        {
            qDebug()<<"lrc 文件格式有问题！！[lrcpage.cpp 62]";
            continue;
        }
        time=line.mid(left,right-left+1);
        text=line.mid(right+1,line.size()-1-right-1).trimmed();//]右边就是这一行的文本

        //先截取分
        right=time.indexOf(':',left);
        totaltime+=time.mid(left+1,right-left-1).toULongLong()*60*1000;

        //截取秒
        left=right+1;
        right=time.indexOf('.',left);
        totaltime+=time.mid(left,right-left).toULongLong()*1000;

        //截取毫秒
        left=right+1;
        right=time.indexOf('.',left);
        totaltime+=time.mid(left,right-left).toULongLong();


        qDebug()<<totaltime<<": "<<text;
        _LrcLines.push_back({totaltime,text});


    }


    //关闭文件
    file.close();
    return true;
}

void LrcPage::showLrcWord(qint64 position)
{

    //根据时间来选择到达了歌词中那个索引
    int index=getLineLrcWordIndex(position);

    if(index==-1)
    {
        ui->line1->setText("");
        ui->line2->setText("");
        ui->line3->setText("");
        ui->lineCenter->setText("暂无歌词信息");
        ui->line4->setText("");
        ui->line5->setText("");
        ui->line6->setText("");
    }
    else
    {
       // qDebug()<<"111111111111111";


        ui->line1->setText(getLrcWordByindex(index-3));
        ui->line2->setText(getLrcWordByindex(index-2));
        ui->line3->setText(getLrcWordByindex(index-1));
        ui->lineCenter->setText(getLrcWordByindex(index));
        ui->line4->setText(getLrcWordByindex(index+1));
        ui->line5->setText(getLrcWordByindex(index+2));
        ui->line6->setText(getLrcWordByindex(index+3));

    }
}


int LrcPage::getLineLrcWordIndex(qint64 pos)
{
    if(_LrcLines.isEmpty())
    {
        return -1;

    }

    if(_LrcLines[0].time>pos)
    {

        return 0;
    }


    for(int i =1;i<_LrcLines.size();i++)
    {
        if(pos>_LrcLines[i-1].time && pos<=_LrcLines[i].time)
         {
            qDebug()<<_LrcLines[i-1].time<<":"<<_LrcLines[i].time;
            return i-1;
        }
    }

    //返回最后一行,当上面都不满足,说明pos已经大于最后一个的位置了,此时歌词就为它
    return _LrcLines.size()-1;

}

QString LrcPage::getLrcWordByindex(int index)
{
    if(index<0||index>=_LrcLines.size())
    {
        return "";
    }

    return _LrcLines[index].text;


}

void LrcPage::SetSigner(const QString &signer)
{
    ui->musicSinger->setText(signer);
}

void LrcPage::SetmusicName(const QString &name)
{
    ui->musicName->setText(name);
}
